package services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.aton.db.SessionFactory;

import models.SystemAuthority;
import models.mappers.SystemAuthorityMapper;

public class AuthorityService {
	/**
	 * 根据菜单查找权限
	 *
	 * @Title: getAuthorityByMenu 
	 * @param menuid
	 * @return
	 * @return: List<SystemAuthority>
	 */
	public static List<SystemAuthority> getAuthorityByMenu(String menuid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			SystemAuthorityMapper mapper = ss.getMapper(SystemAuthorityMapper.class);
            return mapper.getAuthorityByMenu(menuid);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据用户和菜单查找按钮权限
	 *
	 * @Title: getAuthorityByOperAndMenu 
	 * @param menuid
	 * @param operatorid
	 * @return
	 * @return: List<String>
	 */
	public static List<String> getAuthorityByOperAndMenu(
			String menuid, String operatorid) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			SystemAuthorityMapper mapper = ss.getMapper(SystemAuthorityMapper.class);
            return mapper.getAuthorityByOperAndMenu(menuid,operatorid);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据id和用户名检查是否有权限
	 *
	 * @Title: isExist 
	 * @param id 权限id
	 * @param operatorid  用户id
	 * @return
	 * @return: boolean
	 */
	public static boolean isExist(String id, String operatorid) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			SystemAuthorityMapper mapper = ss.getMapper(SystemAuthorityMapper.class);
            String btnid= mapper.isExist(id,operatorid);
            if(StringUtils.isEmpty(btnid)){
            	return false;
            }
            else{
            	return true;
            }
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据菜单id集合获取权限列表
	 *
	 * @Title: getAuthorityByMenus 
	 * @param listid
	 * @return
	 * @return: List<SystemAuthority>
	 */
	public static List<SystemAuthority> getAuthorityByMenus(List<String> listid) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			SystemAuthorityMapper mapper = ss.getMapper(SystemAuthorityMapper.class);
			return mapper.listBymenu(listid);
        } finally {
            ss.close();
        }
	}
	
}
